/*-
 * MTI.java          TP-Message-Type-Indicator (TP-MTI)
 *
 * $LastChangedBy: stein $
 * $LastChangedRevision: 95 $
 * $LastChangedDate: 2007-02-13 16:19:24 +0000 (Tue, 13 Feb 2007) $
 * $HeadURL: svn://localhost/SMS/src/com/googlecode/attention/sms/pdu/MTI.java $
 * 
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.sms.pdu;

/**
 * TP-Message-Type-Indicator (TP-MTI).
 * <BR>
 * The TP-Message-Type-Indicator is a 2-bit field, located within
 * bits no 0 and 1 of the first octet of all PDUs which can be given
 * the following values:
 * <BR>
 * <BR><TABLE>
 * <TR><TD>bit1</TD><TD>bit0</TD><TD>Message type</TD></TR>
 * <TR><TD>0</TD><TD>0</TD><TD>SMS-DELIVER (in the direction SC to MS)</TD></TR>
 * <TR><TD>0</TD><TD>0</TD><TD>SMS-DELIVER REPORT (in the direction MS to SC)</TD></TR>
 * <TR><TD>1</TD><TD>0</TD><TD>SMS-STATUS-REPORT (in the direction SC to MS)</TD></TR>
 * <TR><TD>1</TD><TD>0</TD><TD>SMS-COMMAND (in the direction MS to SC)</TD></TR>
 * <TR><TD>0</TD><TD>1</TD><TD>SMS-SUBMIT (in the direction MS to SC)</TD></TR>
 * <TR><TD>0</TD><TD>1</TD><TD>SMS-SUBMIT-REPORT (in the direction SC to MS)</TD></TR>
 * <TR><TD>1</TD><TD>1</TD><TD>Reserved</TD></TR>
 * </TABLE>
 * <BR>
 * <BR>Abbreviations:
 * <BR>
 * <BR> SC - Service Center
 * <BR> MS - Mobile Station (equiv. "user equipment", e.g. mobile phone)
 * 
 * @author Stein Gunnar Bakkeby
 */
public enum MTI {
  
  /* Static field values */
  /** SC ==> MS */
  SMS_DELIVER(0),
  /** MS ==> SC */
  SMS_DELIVER_REPORT(0),
  /** SC ==> MS */
  SMS_SUBMIT_REPORT(1),
  /** MS ==> SC */
  SMS_SUBMIT(1),
  /** SC ==> MS */
  SMS_STATUS_REPORT(2),
  /** MS ==> SC */
  SMS_COMMAND(2),
  /** Reserved */
  RESERVED(3);
  
  /**
   * This enum represents the direction of the message,
   * used for identifying the correct MTI.
   *
   * @author Stein Gunnar Bakkeby
   */
  public enum DIRECTION {
    /** SC ==> MS */
    SC_TO_MS,
    /** MS ==> SC */
    MS_TO_SC;
  }
  
//  Message type indicator (MTI)
//  The message type indicator, MTI, is a 3-bit field, located in the first octet of all RP-messages. The coding of the MTI is
//  defined by table 8.3/3GPP TS 24.011.
//  Table 8.3/3GPP TS 24.011: Coding of Message Type Indicator
//  Bit value Direction RP-Message
//  3 2 1
//  0 0 0 ms -> n RP-DATA
//  0 0 0 n -> ms Reserved
//  0 0 1 ms -> n Reserved
//  0 0 1 n -> ms RP-DATA
//  0 1 0 ms -> n RP-ACK
//  0 1 0 n -> ms Reserved
//  0 1 1 ms -> n Reserved
//  0 1 1 n -> ms RP-ACK
//  1 0 0 ms -> n RP-ERROR
//  1 0 0 n -> ms Reserved
//  1 0 1 ms -> n Reserved
//  1 0 1 n -> ms RP-ERROR
//  1 1 0 ms -> n RP-SMMA
//  1 1 0 n -> ms Reserved
//  1 1 1 ms -> n Reserved
//  1 1 1 n -> ms Reserved
  
  /** The binary value of the MTI. */
  public final int value;
  
  private MTI(final int value) {
    this.value = value;
  }
  
  /**
   * This will parse an integer and return the matching MTI enum value.
   * 
   * @param value The int value to parse
   * @param direction The direction of the SMS message
   * @return The respective MTI enum value
   */
  public static MTI parse(final int value, final DIRECTION direction) {
    switch (direction) {
    case SC_TO_MS:
      switch (value) {
      case 0: return SMS_DELIVER;
      case 1: return SMS_SUBMIT_REPORT;  
      case 2: return SMS_STATUS_REPORT;
      case 3: return RESERVED;
      default:
        throw new IllegalArgumentException("No MTI value matches " + value);
      }
      
    case MS_TO_SC:
      switch (value) {
      case 0: return SMS_DELIVER_REPORT;
      case 1: return SMS_SUBMIT;
      case 2: return SMS_COMMAND;
      case 3: return RESERVED;
      default:
        throw new IllegalArgumentException("No MTI value matches " + value);
      }
      
    default:
      throw new UnsupportedOperationException(
          "Switch-case not implemented for: " + direction);
    }
  }
}